package com.yzkj.framework.core.filter;

import com.yzkj.framework.beans.exception.YzkjException;
import com.yzkj.framework.beans.result.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常捕获
 */
@RestControllerAdvice
@Component
@Slf4j
public class GlobalExpHandler {

    @ExceptionHandler(YzkjException.class)
    public ApiResponse<Object> trySiotBizException(HttpServletRequest req, HandlerMethod handlerMethod, YzkjException e) {
        log.info("业务系统异常, 可忽略", e);
        return ApiResponse.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ApiResponse<Object> tryRuntimeException(HttpServletRequest req, HandlerMethod handlerMethod, RuntimeException e) {
        log.error("系统运行未知异常", e);
        return ApiResponse.fail();
    }
}
